package com.yyds.oneservice.controller;


import com.yyds.common.R;
import com.yyds.common.constant.SystemConstant;
import com.yyds.common.utils.JwtUtils;
import com.yyds.oneservice.annotation.UnInterception;
import com.yyds.oneservice.pojo.UserMember;
import com.yyds.oneservice.service.IUserMemberService;
import com.yyds.oneservice.vo.LoginVo;
import com.yyds.oneservice.vo.PhoneVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import static com.yyds.common.utils.RegularUtils.checkPhone;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lxy
 * @since 2022-12-12
 */
@Api(tags = "用户使用的登录功能接口")
@RestController
@RequestMapping("/user")
public class UserLoginController {
    @Autowired
    private IUserMemberService userService;

    @PostMapping("/login")
    @ApiOperation(value = "用户账号登录")
    @UnInterception
    public R login(@RequestBody @ApiParam(value = "用户信息") LoginVo loginVo){
        //TODO
        UserMember currentUser = userService.login(loginVo);
        System.out.println(currentUser);
        if(currentUser!=null){
            //生成token 返回给前端
            String token = JwtUtils.createJWT(String.valueOf(currentUser.getId()), currentUser.getUsername(), SystemConstant.JWT_TTL);
            return R.ok().code(20000).data("token",token).data("data",currentUser);
        }else{
            return R.error().message("用户名或密码错误");
        }
    }
    @PostMapping("/phonelogin")
    @ApiOperation(value = "用户手机号登录")
    @UnInterception
    public R phonelogin (@RequestBody @ApiParam(value = "用户手机号信息") PhoneVo phoneVo){
        UserMember currentUser=userService.loginByPhone(phoneVo);
        if(currentUser!=null){
            //生成token 返回给前端
            String token = JwtUtils.createJWT(String.valueOf(currentUser.getId()), currentUser.getUsername(), SystemConstant.JWT_TTL);
            return R.ok().code(20000).data("token",token).data("data",currentUser);
        }else{
            return R.error().message("手机号或者密码错误");
        }
    }

    @PostMapping("/getcaptcha")
    @ApiOperation(value = "获得验证码")
    @UnInterception
    public R getcaptcha (){

        return  R.ok().message("abcdef");
    }
    @PostMapping("/captchalogin")
    @ApiOperation(value = "用户手机号验证码登录")
    @UnInterception
    public R phonelogin ( @ApiParam(value = "用户手机号信息" ,defaultValue = "18061581847") String phone,@ApiParam(value = "验证码")String captcha){

        System.out.println(captcha);

//        userService.findUserByphone(phone);
        if(phone.isEmpty()&&phone==null){
            return R.error().message("请输入手机号和密码");
        }else{
            if(checkPhone(phone)){
                UserMember userByphone = userService.findUserByphone(phone);
                System.out.println(userByphone);
                System.out.println(phone+" 符合");
                return R.ok().data("data",userByphone);
            }else{
                System.out.println(phone+" 不符合");
                return  R.error().message("手机号不符合要求");
            }
        }
    }
}
